package cn.wx.shop.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.wx.shop.entity.Category;
import cn.wx.shop.service.CategoryService;

@Controller
@RequestMapping("/sys/cate")
public class CategoryController {

	@Autowired
	CategoryService categoryService;
	
	@RequestMapping("/list")
	public String cate(Model model){
		List<Category> categorys = categoryService.categorys();
		
		List<Category> parentCategoryList = categoryService.categoryByParentId(0);
		model.addAttribute("parentCategoryList", parentCategoryList);
		model.addAttribute("categoryList", categorys);
		return "category";
	}

	@ResponseBody
	@RequestMapping("/getCates")
	public List<Category> categoryById(Integer id){

		return categoryService.categoryByParentId(id);
	}
	
	@ResponseBody
	@RequestMapping("/add")
	public Integer  cateAdd(Category category){
		return categoryService.addCategory(category);
	}

	@ResponseBody
	@RequestMapping("/del/{id}")
	public Integer cateDel(@PathVariable Integer id){

		return categoryService.delCategory(id);
	}
}

